/*  
*Creates scatterplots of the estimated survey score marginal gains from pictures in range. 


Requires the clean dataset:
survey_level_data.dta

and requires installing the parmest command

Arianna Salazar Miranda
*/
**************************************************************************************************
*Replace with path to data
**************************************************************************************************

clear all
global project "/Users/arianna/Dropbox (MIT)/emporis_project/paper/submission_PLOS/data_repository"


**************************************************************************************************
*open survey database and collapse score means
**************************************************************************************************

use "${project}/Data/survey_level_data.dta", clear

**************************************************************************************************
*cleaning
**************************************************************************************************

drop if officialname=="Mark Twain Building"
drop if officialname=="Empire State Building"
drop if officialname=="Byrd's Lofts"

**************************************************************************************************
*Generate "disk" variables for Panoramio 2014 
**************************************************************************************************

gen rad500_r490=r500_count_picData_2014-r490_count_picData_2014
gen rad490_r480=r490_count_picData_2014-r480_count_picData_2014
gen rad480_r470=r480_count_picData_2014-r470_count_picData_2014
gen rad470_r460=r470_count_picData_2014-r460_count_picData_2014
gen rad460_r450=r460_count_picData_2014-r450_count_picData_2014
gen rad450_r440=r450_count_picData_2014-r440_count_picData_2014
gen rad440_r430=r440_count_picData_2014-r430_count_picData_2014
gen rad430_r420=r430_count_picData_2014-r420_count_picData_2014
gen rad420_r410=r420_count_picData_2014-r410_count_picData_2014
gen rad410_r400=r410_count_picData_2014-r400_count_picData_2014

gen rad400_r390=r400_count_picData_2014-r390_count_picData_2014
gen rad390_r380=r390_count_picData_2014-r380_count_picData_2014
gen rad380_r370=r380_count_picData_2014-r370_count_picData_2014
gen rad370_r360=r370_count_picData_2014-r360_count_picData_2014
gen rad360_r350=r360_count_picData_2014-r350_count_picData_2014
gen rad350_r340=r350_count_picData_2014-r340_count_picData_2014
gen rad340_r330=r340_count_picData_2014-r330_count_picData_2014
gen rad330_r320=r330_count_picData_2014-r320_count_picData_2014
gen rad320_r310=r320_count_picData_2014-r310_count_picData_2014
gen rad310_r300=r310_count_picData_2014-r300_count_picData_2014

gen rad300_r290=r300_count_picData_2014-r290_count_picData_2014
gen rad290_r280=r290_count_picData_2014-r280_count_picData_2014
gen rad280_r270=r280_count_picData_2014-r270_count_picData_2014
gen rad270_r260=r270_count_picData_2014-r260_count_picData_2014
gen rad260_r250=r260_count_picData_2014-r250_count_picData_2014
gen rad250_r240=r250_count_picData_2014-r240_count_picData_2014
gen rad240_r230=r240_count_picData_2014-r230_count_picData_2014
gen rad230_r220=r230_count_picData_2014-r220_count_picData_2014
gen rad220_r210=r220_count_picData_2014-r210_count_picData_2014
gen rad210_r200=r210_count_picData_2014-r200_count_picData_2014

gen rad200_r190=r200_count_picData_2014-r190_count_picData_2014
gen rad190_r180=r190_count_picData_2014-r180_count_picData_2014
gen rad180_r170=r180_count_picData_2014-r170_count_picData_2014
gen rad170_r160=r170_count_picData_2014-r160_count_picData_2014
gen rad160_r150=r160_count_picData_2014-r150_count_picData_2014
gen rad150_r140=r150_count_picData_2014-r140_count_picData_2014
gen rad140_r130=r140_count_picData_2014-r130_count_picData_2014
gen rad130_r120=r130_count_picData_2014-r120_count_picData_2014
gen rad120_r110=r120_count_picData_2014-r110_count_picData_2014
gen rad110_r100=r110_count_picData_2014-r100_count_picData_2014

gen rad100_r90=r100_count_picData_2014-r90_count_picData_2014
gen rad90_r80=r90_count_picData_2014-r80_count_picData_2014
gen rad80_r70=r80_count_picData_2014-r70_count_picData_2014
gen rad70_r60=r70_count_picData_2014-r60_count_picData_2014
gen rad60_r50=r60_count_picData_2014-r50_count_picData_2014
gen rad50_r40=r50_count_picData_2014-r40_count_picData_2014
gen rad40_r30=r40_count_picData_2014-r30_count_picData_2014
gen rad30_r20=r30_count_picData_2014-r20_count_picData_2014
gen rad20_r10=r20_count_picData_2014-r10_count_picData_2014
gen rad10_r0=r10_count_picData_2014

**************************************************************************************************
*Generate mean score variable
**************************************************************************************************

bysort buildingnumberebn: egen mean_score = mean(score)


**************************************************************************************************
*Regression of all "disk" variables on score
**************************************************************************************************

reg score rad*, cluster(buildingnumberebn)
parmest,  format(estimate min95 max95) saving(est.dta, replace)

preserve
use est.dta, clear 
split parm, p("_")
destring parm1 , gen(radii) i(r a d ) force
replace radii=160 in 35
replace estimate=. if radii==160
drop if parm=="_cons"

tsset radii, delta(10)

tssmooth ma ma_est=estimate, window(5 1 0)


**************************************************************************************************
*Figure 3 (left panel)
**************************************************************************************************

twoway (line ma_est radii) ///
	   (rcap min95 max95 radii, legend(size(vsmall))  lcolor(eltblue) lwidth(thin)) ///
	   (scatter estimate radii, msymbol(circle_hollow) mcolor(edkblue) lwidth(thin)) ///
	   (lfit estimate radii, lcolor(cranberry) lpattern(dash)) ///
	   (qfit estimate radii, lcolor(gs12) lpattern(dash)), ///
	   plotregion(fcolor(white) lcolor(black) lwidth(vvthin)) ///
	   graphregion(fcolor(white) lcolor(white) lwidth(vvthin)) ///
	   xtitle("Distance (meters)", size(3) margin(medium)) xscale(noline) xlabel(, labsize(small) labcolor(gs11) labgap(small) noticks grid glwidth(vthin) glcolor(gs13) gextend) legend(nobox) ///
	   ytitle("Change in survey scores", margin(medium)) yscale(noline) ylabel(#5, labsize(small) labcolor(gs11) labgap(vsmall) ticks tlcolor(gs13) grid glwidth(vthin) glcolor(gs13) gextend) ///
	   yline(0,lcolor(black) lwidth(thin)) ///
	   legend(on rows(2) order(3 "Estimated score gains" 1 "Moving average" 4 "Linear fit" 5 "Quadratic fit") region(fcolor(white) lcolor(none))) ///
	   xsize(3) ysize(4) scale(1) ylabel(-.2(.2).5)
restore

 
**************************************************************************************************
*Figure 3 (right panel)
**************************************************************************************************

reg score rad*, cluster(buildingnumberebn)
parmest,  format(estimate min95 max95) saving(est.dta, replace)

preserve
use est.dta, clear 
split parm, p("_")
destring parm1 , gen(radii) i(r a d ) force
replace radii=160 in 35
replace estimate=. if radii==160
drop if parm=="_cons"

tsset radii, delta(10)

tssmooth ma ma_est=estimate, window(5 1 0)

keep if radii <=150
twoway (line ma_est radii) ///
	   (rcap min95 max95 radii, legend(size(vsmall))  lcolor(eltblue) lwidth(thin)) ///
	   (scatter estimate radii, msymbol(circle_hollow) mcolor(edkblue) lwidth(thin)) ///
	   (lfit estimate radii, lcolor(cranberry) lpattern(dash)) ///
	   (qfit estimate radii, lcolor(gs12) lpattern(dash)), ///
	   plotregion(fcolor(white) lcolor(black) lwidth(vvthin)) ///
	   graphregion(fcolor(white) lcolor(white) lwidth(vvthin)) ///
	   xtitle("Distance (meters)", size(3) margin(medium)) xscale(noline) xlabel(, labsize(small) labcolor(gs11) labgap(small) noticks grid glwidth(vthin) glcolor(gs13) gextend) legend(nobox) ///
	   ytitle("Change in survey scores", margin(medium)) yscale(noline) ylabel(#5, labsize(small) labcolor(gs11) labgap(vsmall) ticks tlcolor(gs13) grid glwidth(vthin) glcolor(gs13) gextend) ///
	   yline(0,lcolor(black) lwidth(thin)) ///
	   legend(on rows(2) order(3 "Estimated score gains" 1 "Moving average" 4 "Linear fit" 5 "Quadratic fit") region(fcolor(white) lcolor(none))) ///
	   xsize(3) ysize(4) scale(1) ylabel(-.2(.2).5)
restore


